<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/18 0018
 * Time: 下午 14:10
 */

namespace frontend\models\c2\entity;

use common\helpers\VoteHelper;
use common\models\c2\entity\ActivityModel;
use common\models\c2\entity\ActivityPlayerVoteRecordModel;
use common\models\c2\statics\Whether;
use cza\base\models\statics\ResponseDatum;
use Yii;

class VoteRecordModel extends ActivityPlayerVoteRecordModel
{


    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if ($insert) {
            $playerModel = $this->player;
            $playerModel->updateCounters(['free_vote_number' => 1]);
            $playerModel->updateCounters(['total_vote_number' => 1]);
            $playerModel->activity->updateCounters(['vote_number' => 1]);
            $this->user->updateCounters(['score' => 1]);
            $redis = \Yii::$app->redis;
            $key = VoteHelper::getVotedKey($this->user_id, $this->activity_player_id);
            $redis->incr($key);
            $redis->expire($key, 60 * 60 * 24);
            VoteHelper::setActivityPlayerRank($redis, $playerModel);
        }
    }

}